home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
awe2-0_1.lha
/
awe2-0.1
/
Src
/
RCS
/
GenericKeylessHeap.h,v
< prev
next >
Wrap
Text File
|
1988-10-12
|
3KB
|
166 lines
head 1.1;
access ;
symbols ;
locks grunwald:1.1; strict;
comment @ * @;
1.1
date 88.09.18.16.42.06; author grunwald; state Exp;
branches ;
next ;
desc
@@
1.1
log
@Initial revision
@
text
@//
// Define the following things:
// HEAP_NAME -- the name of the heap type
// HEAP_INDEX -- the index type; defaults to unsigned short
// HEAP_INDEX_NULL -- the null value for the heap index; defaults to 65535
// HEAP_BASE_CLASS -- the base class for the new heap
//
// Furthermore, the item type should be a type with the name
// GENERIC2(HEAP_NAME,Item)
//
// This type can be anything (simple, class, typedef) that provides
// comparison operatiohns.
//
#include "Awesime.h"
#include "Generic.h"
#ifndef HEAP_INDEX
#define HEAP_INDEX unsigned short
#endif
#ifndef HEAP_INDEX_NULL
#define HEAP_INDEX_NULL 0xffff
#endif
#ifndef HEAP_BASE_CLASS
#define HEAP_BASE_CLASS public Awesime
#endif
//
// The following are simplifications of the preceeding names;
// they make the following code much more readable
//
// However, for safety, they are undef'd at the end of the .h file,
// so you'll need to re-def them in your .cc file if you want to use them.
//
#define NIL GENERIC2(HEAP_NAME,Null)
#define INDEX GENERIC2(HEAP_NAME,Index)
#define ITEM GENERIC2(HEAP_NAME,Item)
typedef HEAP_INDEX INDEX;
extern const INDEX NIL;
typedef struct {
ITEM item;
INDEX sibling;
INDEX children;
} GENERIC2(HEAP_NAME,Record);
class HEAP_NAME : HEAP_BASE_CLASS {
GENERIC2(HEAP_NAME,Record) *storage;
char *pValid;
int elements;
INDEX allocatedSize;
INDEX freelist;
INDEX root;
void makeRoom(int atLeast);
INDEX makeChild(INDEX a, INDEX b);
inline INDEX getCell() {
if (freelist == GENERIC2(HEAP_NAME,Null)) {
makeRoom(0);
}
INDEX cell = freelist;
pValid[freelist] = 1;
freelist = storage[freelist].sibling;
elements++;
return(cell);
}
inline void returnCell(INDEX cell) {
storage[cell].sibling = freelist;
pValid[cell] = 0;
freelist = cell;
elements--;
}
public:
HEAP_NAME(int length = 0, bool xdebug = 0);
void add(ITEM &item);
bool remove(ITEM &removed);
//
// The next four members allow you to search a HEAP_NAME and mark
// items as invalid.
//
// doStart initilizes the index and returns the first item in the list.
// doNext moves to the next item and returns that item.
// Both return a bool indicating whether the value in item has any
// meaning. When bool=FALSE, you've searched everything.
// Call doDone at the end to insure compatibility with subclasses.
//
virtual bool doStart( INDEX &index);
virtual bool doDelete(INDEX &index);
virtual bool doNext( INDEX &index);
virtual void doDone();
inline INDEX minItem() {
return(root);
};
inline bool valid(INDEX index) {
return ( pValid[index] );
}
inline ITEM & item(INDEX i) {
return( storage[i].item );
}
inline INDEX maxIndex() {
return(allocatedSize);
}
inline bool isEmpty() {
return(elements <= 0);
}
inline unsigned int size() {
return(elements);
}
virtual void classPrintOn(ostream& s);
};
#undef INDEX
#undef ITEM
#undef NIL
#undef HEAP_NAME
#undef HEAP_ITEM
#undef HEAP_INDEX
#undef HEAP_INDEX_NULL
#undef HEAP_BASE_CLASS
@